package com.base.framework.admin.api.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.base.framework.admin.domain.Account;
import com.base.framework.admin.service.IAccountService;
import com.base.framework.admin.vo.system.LoginVo;
import com.base.framework.common.response.ActionResponse;
import com.base.framework.common.utils.PasswordUtil;

@RestController
@RequestMapping("/system/account/")
public class AccountController {
	  @Autowired
	  private IAccountService accountService;
	  /*
	   * 功能：用户登录
	   */
	  @RequestMapping("login")
      public ActionResponse<Boolean>  login(LoginVo loginVo){
		   /*
		    * 1.验证参数(包括验证码)
		    * 2.查询用户名是否存在
		    * 3.验证密码
		    * 4.授权
		    * 5.返回
		    * */
		    ActionResponse<Boolean> response=new ActionResponse<Boolean>();
    	    if(StringUtils.isEmpty(loginVo.getPhone()) || StringUtils.isEmpty(loginVo.getPassword())){
    	    	response.failMessage("手机号或密码为空!");
    	    	return response;
    	    }
    	    //
    	    Account queryAccount=new Account();
    	    queryAccount.setPhone(loginVo.getPhone());
    	    Account resultAccount =accountService.selectOne(queryAccount);
    	    if(null==resultAccount){
    	    	response.failMessage("用户名或密码错误!");
    	    	return response;
    	    }
    	    if(!PasswordUtil.md5Hex(loginVo.getPassword()).equals(resultAccount.getPassword())){
    	    	response.failMessage("用户名或密码错误!");
    	    	return response;
    	    }
    	    response.success(true);
    	    return response;
      } 
	  /*
	   * 功能：用户退出
	   */
	  @RequestMapping("loginOut")
      public ActionResponse<Boolean>  loginOut(){
		  /*
		    * 1.取消授权(todo)
		    * */
		  ActionResponse<Boolean> response=new ActionResponse<Boolean>();
		  response.success(true);
		  return response;
	  }
	  
	  /*
	   * 功能：新增用户
	   * 参数：姓名、手机、图像、角色
	   */
	  @RequestMapping("add")
      public ActionResponse<Boolean>  add(Account account){
		  /*
		    * 1.取消授权(todo)
		    * */
		  ActionResponse<Boolean> response=new ActionResponse<Boolean>();
		  account.setStatus("1");
		  account.setIsAdmin("2");
		  account.setCreateTime(new Date());
		  //设置创建人 todo
		  boolean isOk=accountService.insertSelective(account);
		  if(!isOk){
			  response.failMessage("新增用户失败！");
			  return response;
		  }
		  response.success(true);
		  return response;
	  }
	  
	  /*
	   * 功能：查询用户
	   */
	  @RequestMapping("list")
      public ActionResponse<Page<Account>>  list(){
		  ActionResponse<Page<Account>> response=new ActionResponse< Page<Account>>();
		  Page<Account> page=new Page<Account>();
		  EntityWrapper<Account> entityWrapper=new EntityWrapper<Account>();
		  Page<Account> resultPage=accountService.selectPage(page, entityWrapper);
		  response.success(resultPage);
		  return response;
	  }
	  
	  /*
	   * 功能：更新用户
	   */
	  /*
	   * 功能：查询用户
	   */
	  @RequestMapping("edit")
      public ActionResponse<Boolean>  edit(Account account){
		  ActionResponse<Boolean> response=new ActionResponse<Boolean>();
		  boolean isOk=accountService.updateSelectiveById(account);
		  if(!isOk){
			  response.failMessage("编辑用户失败！");
			  return response;
		  }
		  response.success(true);
		  return response;
	  }
	  /*
	   * 功能：批量删除用户
	   */
	  @RequestMapping("delete")
      public ActionResponse<Boolean>  delete(String idsStr){
		  String[] ids=idsStr.split(",");
		  List<Long> idsList=new ArrayList<Long>();
		  for(String id : ids){
			  idsList.add(Long.valueOf(id));
		  }
		  ActionResponse<Boolean> response=new ActionResponse<Boolean>();
		  boolean isOk=accountService.deleteBatchIds(idsList);
		  if(!isOk){
			  response.failMessage("编辑用户失败！");
			  return response;
		  }
		  response.success(true);
		  return response;
	  }
	  
	  /*
	   * 功能：查看用户
	   */
	  @RequestMapping("view")
      public ActionResponse<Account>  view(Long id){
		  ActionResponse<Account> response=new ActionResponse<Account>();
		  Account account=accountService.selectById(id);
		  if(null==account){
			  response.failMessage("用户不存在！");
			  return response;
		  }
		  response.success(account);
		  return response;
	  }
	  
}
